package my.service.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Cart;
import my.dao.ICartDao;
import my.dao.Impl.ICartDaoImpl;
import my.service.ICartService;

/**
 * 业务实现类,通过调用数据访问层代码,实现数据处理功能
 * 
 * @author slim
 *
 */
public class ICartServiceImpl implements ICartService {

	private ICartDao cartDao;

	@Override
	public int add(Cart cart) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			cartDao = new ICartDaoImpl(conn);
			record = cartDao.add(cart);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误,尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteById(int cid) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			cartDao = new ICartDaoImpl(conn);
			record = cartDao.deleteById(cid);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误,尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modify(Cart cart) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			cartDao = new ICartDaoImpl(conn);
			record = cartDao.modify(cart);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误,尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Cart> findAll() {
		Connection conn = null;
		List<Cart> list = null;
		try {
			conn = DBUtils.getConnection();
			cartDao = new ICartDaoImpl(conn);
			list = cartDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Cart> findByUserId(int userid) {
		Connection conn = null;
		List<Cart> list = null;
		try {
			conn = DBUtils.getConnection();
			cartDao = new ICartDaoImpl(conn);
			list = cartDao.findByUserId(userid);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Cart findByCartId(int cartId) {
		Connection conn = null;
		Cart cart = null;
		try {
			conn = DBUtils.getConnection();
			cartDao = new ICartDaoImpl(conn);
			cart = cartDao.findByCartId(cartId);
			return cart;
		} finally {
			DBUtils.close(conn, null, null);
		}

	}

}
